package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.cms.common.exception.CmsException;
import com.briup.cms.common.model.entity.User;
import com.briup.cms.common.model.ext.UserExt;
import com.briup.cms.common.util.GlobalConstants;
import com.briup.cms.common.util.PageUtil;
import com.briup.cms.common.util.ResultCode;
import com.briup.cms.common.util.SecurityUtil;
import com.briup.cms.dao.UserMapper;
import com.briup.cms.service.UserService;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * @author YuYan
 * @date 2024-01-09 19:42:40
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;
    private final SecurityUtil securityUtil;
    @Override
    public void save(@NotNull UserExt userExt) {
        String username=userExt.getUsername();
        String password=userExt.getPassword();
        String phone=userExt.getPhone();
        String email=userExt.getEmail();
        String gender=userExt.getGender();
        Date birthday=userExt.getBirthday();
        String avatar = userExt.getAvatar();
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,username);
        User record=userMapper.selectOne(lqw);
        if(record!=null){
            throw  new CmsException(ResultCode.USER_HAS_EXISTED);
        }
        password=securityUtil.bcryptEncode(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setEmail(email);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setAvatar(avatar);
        user.setRegisterTime(new Date());
        user.setStatus(GlobalConstants.USER_DEFAULT_STATUS);
        user.setRoleId(GlobalConstants.USER_DEFAULT_ROLE_ID);
        user.setVip(GlobalConstants.USER_DEFAULT_VIP);
        user.setDeleted(GlobalConstants.LOGIC_DELETED_FLAG_VALUE);
        userMapper.insert(user);
    }

    @Override
    public UserExt getById(Long id) {
        return  UserExt.toExt(userMapper.selectById(id));
    }

    @Override
    public UserExt getByIdNullable(Long id) {
        return null;
    }

    @Override
    public void update(UserExt userExt) {
        Long id = userExt.getId();
        String avatar = userExt.getAvatar();
        String username = userExt.getUsername();
        String email = userExt.getEmail();
        String phone = userExt.getPhone();
        String gender = userExt.getGender();
        Date birthday = userExt.getBirthday();
        User user=new User();
        user.setId(id);
        user.setAvatar(avatar);
        user.setUsername(username);
        user.setEmail(email);
        user.setPhone(phone);
        user.setGender(gender);
        user.setBirthday(birthday);
        userMapper.updateById(user);

    }

    @Override
    public void delete(List<Long> ids) {
        userMapper.deleteBatchIds(ids);
    }

    @Override
    public IPage<UserExt> pageQueryByClause(UserExt userExt, IPage<User> page) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.hasText(userExt.getUsername()), User::getUsername, userExt.getUsername());
        lqw.eq(userExt.getRoleId() != null, User::getRoleId, userExt.getRoleId());
        lqw.eq(userExt.getVip() != null, User::getVip, userExt.getVip());
        lqw.eq(StringUtils.hasText(userExt.getStatus()), User::getStatus, userExt.getStatus());
        userMapper.selectPage(page, lqw);
        IPage<UserExt> newPage = PageUtil.convert(page, UserExt::toExt);
        return newPage;
    }

    @Override
    public UserExt getByUsername(String username) {
        return UserExt.toExt(userMapper.selectById(null));
    }

    @Override
    public List<UserExt> list() {
        return UserExt.toExt(userMapper.selectList(null));
    }
}
